import useChatStore from '@/store/chatStore.js'
import useFriendStore from '@/store/friendStore.js'
import useUserStore from '@/store/userStore.js'
import useConfigStore from '@/store/configStore.js'

const refreshFriendBadge = () => {
	let friendStore = useFriendStore();
	let userStore = useUserStore();
	let configStore = useConfigStore();
	let userId = userStore.userInfo.id;
	let count = friendStore.requests.filter((req) => req.recvId == userId).length;
	setBadge(1, count);
}

const refreshChatBadge = () => {
	let chatStore = useChatStore();
	let userStore = useUserStore();
	let configStore = useConfigStore();
	let count = 0;
	chatStore.chats.forEach(chat => {
		if (!chat.delete) {
			count += chat.unreadCount;
		}
	})
	setBadge(0, count);
	// Android平台调用原生API设置角标
	// #ifdef APP-PLUS
	if (uni.getSystemInfoSync().platform === 'android') {
		plus.runtime.setBadgeNumber(count);
	}
	// #endif
}

const setBadge = (idx, count) => {
	// 不使用TabBar徽章，改用全局状态存储未读数量
	let configStore = useConfigStore();
	if (idx === 0) {
		configStore.chatUnreadCount = count;
	} else if (idx === 1) {
		configStore.friendUnreadCount = count;
	}
}

export {
	refreshChatBadge,
	refreshFriendBadge
}